/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Dec 13, 2009 10:42:21 PM $
 *
 * Author: Eric Yang
 * Date  : Dec 13, 2009 10:42:21 PM
 *
 */
package com.gooex.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.struts2.ServletActionContext;
import org.c4j.net.URLTools;
import org.c4j.string.StringTools;
import org.c4j.system.web.Constants;
import org.c4j.system.web.localize.Localizer;
import org.c4j.system.web.page.Page;

import com.gooex.domain.base.Category;
import com.gooex.domain.common.Region;
import com.gooex.domain.organization.User;
import com.gooex.service.base.ICategoryManager;
import com.gooex.service.base.IPropertyManager;
import com.gooex.service.common.IAppMessageManager;
import com.gooex.service.organization.AuthorityContext;
import com.gooex.service.organization.IApplicationSessionContext;
import com.gooex.service.organization.IUserManager;
import com.gooex.web.action.support.SuccessPage;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Eric Yang
 * @version 1.0
 */
public class BaseAction extends ActionSupport {

	private static final String SUCCESS_PAGE = "userCenterSuccessPage";

	protected IApplicationSessionContext sessionContext;

	protected IUserManager userManager;

	protected ICategoryManager categoryManager;

	protected IPropertyManager propertyManager;

	protected IAppMessageManager appMessageManager;

	protected Page page;

	/** If it's true, action should record the URL coming from and provide link on page to go back */
	protected boolean back = false;

	public final Map<String, String> getBackURL() {
		return getBackURLs().peek();
	}

	/**
	 * 加入一个回退链接
	 */
	public final void setBackURL(String linkAddr) {
		HashMap<String, String> url = new HashMap<String, String>();
		url.put(IApplicationSessionContext._LINK_ADDR, linkAddr);
		url.put(IApplicationSessionContext._LINK_TEXT, "返回");
		getBackURLs().add(url);
	}

	/**
	 * 获取跳转会去的链接queue，如果没有，则创建一个
	 */
	public final Queue<Map<String, String>> getBackURLs() {
		Queue<Map<String, String>> queue = (Queue<Map<String, String>>) sessionContext
				.getSession(IApplicationSessionContext._BACK_URLS);
		if (queue == null) {
			queue = new ConcurrentLinkedQueue<Map<String, String>>();
			sessionContext.setSession(IApplicationSessionContext._BACK_URLS, queue);
		}
		return queue;
	}

	public int getUnreadMsgCount() {
		String key = "_unread_app_msg_count";
		Object val = sessionContext.getSession(key);
		if (val != null)
			return ((Integer) val);

		int unreadMsgCount = appMessageManager.getUnreadMsgCount(getCurrentUser().getEmail());
		if (unreadMsgCount == 0) {
			sessionContext.setSession(key, unreadMsgCount);
		}
		return unreadMsgCount;
	}

	public boolean isBack() {
		return back;
	}

	public void setBack(boolean back) {
		this.back = back;
	}

	public final User getCurrentUser() {
		return userManager.getUserByAccount((String) ActionContext.getContext().getSession().get(Constants.ACCOUNT));
	}

	public final String getAccount() {
		User user = getCurrentUser();
		return user == null ? null : user.getName();
	}

	public final void setCurrentUser(User user) {
		sessionContext.setSessionFlag(user);
	}

	public final Page getPage() {
		if (page == null)
			page = new Page();
		return page;
	}

	public final void setPage(Page page) {
		this.page = page;
	}

	public final String getOriginalURL() {
		return URLTools.buildOriginalURL(ServletActionContext.getRequest());
	}

	public final String getReferringURL() {
		return getOriginalURL();
	}

	public final String getClientIPAddress() {
		return ServletActionContext.getRequest().getRemoteAddr();
	}

	public final boolean isSystemObject(Object object) {
		return AuthorityContext.isSystemObject(object);
	}

	public final boolean isSystemUser(User user) {
		return AuthorityContext.isSystemUser(user);
	}

	public final boolean isSystemUser() {
		User currentUser = getCurrentUser();
		return currentUser != null && isSystemUser(currentUser);
	}

	public final String toHtml(String s) {
		return StringTools.convert2HTML(s);
	}

	public final String i18n(String key) {
		return Localizer.defaultLocalizer().getText(key);
	}

	public final Region getCurrentRegion() {
		return sessionContext.getRegion();
	}

	public final List<Category> getTopCategories() {
		return categoryManager.getMenuTopCategories();
	}

	public final List<Category> getChildren(Category parent) {
		return categoryManager.getCategories(parent);
	}

	public final List<Category> getCateChildren(String id) {
		return categoryManager.getCateChildren(id);
	}

	public final List<Category> getChildren(Category parent, int num) {
		return categoryManager.getCategories(parent, num);
	}

	public final void setPropertyManager(IPropertyManager propertyManager) {
		this.propertyManager = propertyManager;
	}

	public final List<Category> getSubCategories(Category category) {
		return categoryManager.getCategories(category);
	}

	public final void setCategoryManager(ICategoryManager categoryManager) {
		this.categoryManager = categoryManager;
	}

	public final void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}

	public final void setSessionContext(IApplicationSessionContext sessionContext) {
		this.sessionContext = sessionContext;
	}

	private SuccessPage successPage;

	public final String userCenterSuccessPage(String url, String desc) {
		this.successPage = new SuccessPage(url, desc);
		return SUCCESS_PAGE;
	}

	public final SuccessPage getSuccessPage() {
		return successPage;
	}

	public final void setAppMessageManager(IAppMessageManager appMessageManager) {
		this.appMessageManager = appMessageManager;
	}
}
